// pages/chef-home/chef-home.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    params: {
      page: 1
    },
    params1: {
      page: 1
    },
    params2: {
      page: 1
    },
    foodList: [],
    foodList2: [],
    foodList3: [],
    active: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.data) {
      let {
        type
      } = JSON.parse(options.data)
      if (type === 3) {
        type = 1
      }
      this.setData({
        active: type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShufflingsData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getShufflingsData() {
    app.reqFetch.stockorderHome({

    }, (res) => {
      const {
        list = []
      } = res.list
      this.setData({
        foodList: list,
        pending: res.pending
      })
    })
    app.reqFetch.collarhome({}, (res) => {

      this.setData({
        foodList2: res.list.list,
        pending1: res.pending
      })
    })
    app.reqFetch.smallcanteenpurchase({}, (res) => {
      const {
        list = []
      } = res.list
      this.setData({
        foodList3: list,
        pending2: res.pending
      })
    })

  },
  loadMore(e) {
    let {
      params,
      foodList
    } = this.data
    this.setData({
      'params.page': params.page + 1
    }, () => {
      app.reqFetch.stockorderHome({
        ...this.data.params
      }, (res) => {
        const {
          list = []
        } = res.list
        this.setData({
          foodList: foodList.concat(list)
        })
      })
    })
  },
  loadMore1(e) {
    let {
      params1,
      foodList2
    } = this.data
    this.setData({
      'params1.page': params1.page + 1
    }, () => {
      app.reqFetch.collarhome({
        ...this.data.params1
      }, (res) => {
        const {
          list = []
        } = res.list
        this.setData({
          foodList2: foodList2.concat(list)
        })
      })
    })
  },
  loadMore2(e) {
    let {
      params2,
      foodList3
    } = this.data
    this.setData({
      'params2.page': params2.page + 1
    }, () => {
      app.reqFetch.stockorderHome({
        ...this.data.params2
      }, (res) => {
        const {
          list = []
        } = res.list
        this.setData({
          foodList3: foodList3.concat(list)
        })
      })
    })
  },

  toDetails(e) {
    const {
      stockOrderId,
      status,
      smallCanteenPurchaseId,
      collarId,
      flag
    } = e.currentTarget.dataset.record
    const type = e.currentTarget.dataset.type
    const order = {
      stockOrderId,
      smallCanteenPurchaseId,
      collarId
    }
    app.globalData.revise = false
    if (type === 0) {
      app.globalData.applyType = 0
    } else if (type === 1) {
      app.globalData.applyType = 3
    } else if (type === 2) {
      app.globalData.applyType = 2
    }


    if (flag === 1) {
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({}),
      })
      return
    }
    if (status == 1) {
      app.globalData.revise = true
      if (type === 0) {
        app.globalData.applyType = 0
        app.globalData.stockOrderId = stockOrderId
        this.getDetails(stockOrderId)
      } else if (type === 1) {
        app.globalData.applyType = 3
        app.globalData.collarId = collarId
        this.getDetails3(collarId)
      } else {
        app.globalData.applyType = 2
        app.globalData.smallCanteenPurchaseId = smallCanteenPurchaseId
        this.getDetails2(smallCanteenPurchaseId)
      }

    } else if (status == 2) {
      wx.navigateTo({
        url: '/pages/admin-purchase-list/admin-purchase-list?data=' + JSON.stringify({
          ...order,
          chenck: true,
          type
        }),
      })
    } else if (status == 3) {
      wx.navigateTo({
        url: '/pages/admin-purchase-list/admin-purchase-list?data=' + JSON.stringify({
          ...order,
          isQuote: true,
          type
        }),
      })
    } else if (status == 4) {
      wx.navigateTo({
        url: '/pages/chef-details/chef-details?data=' + JSON.stringify({
          ...order,
          audit: true,
          type
        }),
      })
    } else if (status == 5) {
      wx.navigateTo({
        url: '/pages/chef-details/chef-details?data=' + JSON.stringify({
          ...order,
          type
        }),
      })

    }
  },


  application() {

    const {
      foodList
    } = this.data
    app.globalData.applyType = 0
    app.globalData.revise = false
    app.globalData.orderList = []

    if (foodList[0] && foodList[0].flag === 1) {
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({}),
      })
    } else if (foodList.length > 0 && !foodList[0].practicalNum) {
      const {
        mealTime,
        estimateNum,
        stockOrderId
      } = foodList[0]
      wx.navigateTo({
        url: '/pages/application-number/application-number?data=' + JSON.stringify({
          mealTime,
          estimateNum,
          stockOrderId
        }),
      })
    } else {
      wx.navigateTo({
        url: '/pages/application-number-reality/application-number-reality',
      })
    }





  },
  application3() {
    app.globalData.applyType = 2
    app.globalData.orderList = []
    const {
      foodList3
    } = this.data
    if (foodList3[0] && foodList3[0].flag === 1) {
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({}),
      })
    } else {
      wx.navigateTo({
        url: '/pages/chef-canteen/chef-canteen',
      })
    }

  },
  application2() {
    app.globalData.applyType = 3
    app.globalData.orderList = []
    const {
      foodList2
    } = this.data
    if (foodList2[0] && foodList2[0].flag === 1) {
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({}),
      })
    } else {
      wx.navigateTo({
        url: '/pages/supply-menu/supply-menu',
      })
    }


  },
  onChange(event) {
    if (event.detail === 1) {
      wx.redirectTo({
        url: '/pages/mine/mine',
      })
    }
  },



  getDetails(stockOrderId) {
    app.reqFetch.getOrderDetails({
      stockOrderId
    }, (res) => {
      const {
        stockorder,
        list
      } = res
      const {
        mealTime,
        estimateNum,
        stockOrderId
      } = stockorder
      let orderList = []
      list.map(item => {
        orderList.push({
          supplierId: item.gysId,
          supplierName: item.gys,
          foods: item.mxlist
        })
      })
      app.globalData.orderList = orderList
      app.globalData.estimatedTime = mealTime
      app.globalData.estimatedNum = estimateNum
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({})
      })

      console.log(mealTime, estimateNum)
    })
  },
  getDetails2(smallCanteenPurchaseId) {
    app.reqFetch.smallcanteenpurchasemxdetails({
      smallCanteenPurchaseId
    }, (res) => {
      const {
        smallCanteenPurchase,
        list
      } = res

      const {
        boxId,
        smallCanteenPurchaseId
      } = smallCanteenPurchase
      let orderList = []
      list.map(item => {
        orderList.push({
          supplierId: item.gysId,
          supplierName: item.gys,
          foods: item.mxlist
        })
      })

      app.globalData.canteensList = JSON.parse(boxId).list
      app.globalData.orderList = orderList

      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({
          smallCanteenPurchaseId,
          revise: true
        })
      })
    })
  },
  getDetails3(collarId) {
    app.reqFetch.collarmxdetails({
      collarId
    }, (res) => {
      const {
        collar,
        collarMx
      } = res
      let orderList = []

      orderList.push({
        supplierName: "主食领取清单",
        foods: collarMx
      })

      app.globalData.orderList = orderList
      wx.navigateTo({
        url: '/pages/purchase-list/purchase-list?data=' + JSON.stringify({
          collarId,
          revise: true
        })
      })
    })

  }
})